package com.ccl.iot.dvr.utils;

import android.annotation.TargetApi;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.os.Build;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.ccl.iot.App;
import com.ccl.iot.R;
import com.ccl.iot.bean.ArmZoneInfoBean;
import com.ccl.iot.device.IOTDevice;
import com.ccl.iot.device.IOTSecurityArm;
import com.ccl.iot.dialogs.CustomDialog;
import com.ccl.iot.publics.UIUtils;
import com.ccl.iot.views.WaitRegView;

import java.util.ArrayList;
import java.util.List;

import static android.view.View.GONE;

/**
 * Created by HD on 2017/2/10.
 */

public class CommonUtils {

    /**
     * 判断底部navigator是否已经显示
     *
     * @param windowManager
     * @return
     */
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    public static boolean hasSoftKeys(WindowManager windowManager) {
        Display display = windowManager.getDefaultDisplay();

        DisplayMetrics realDisplayMetrics = new DisplayMetrics();
        display.getRealMetrics(realDisplayMetrics);

        int realHeight = realDisplayMetrics.heightPixels;
        int realWidth = realDisplayMetrics.widthPixels;

        DisplayMetrics displayMetrics = new DisplayMetrics();
        display.getMetrics(displayMetrics);

        int displayHeight = displayMetrics.heightPixels;
        int displayWidth = displayMetrics.widthPixels;

        return (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0;
    }

    /**
     * 获取手机尺寸（包括虚拟按钮）
     *
     * @param windowManager
     * @return
     */
    public static DisplayMetrics getRealMetrics(WindowManager windowManager) {
        Display display = windowManager.getDefaultDisplay();

        DisplayMetrics realDisplayMetrics = new DisplayMetrics();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            display.getRealMetrics(realDisplayMetrics);
        }
        return realDisplayMetrics;
    }

    /**
     * 获取手机尺寸（不包括虚拟按钮部分）
     *
     * @param windowManager
     * @return
     */
    public static DisplayMetrics getMetrics(WindowManager windowManager) {
        Display display = windowManager.getDefaultDisplay();
        DisplayMetrics displayMetrics = new DisplayMetrics();
        display.getMetrics(displayMetrics);
        return displayMetrics;
    }

    /**
     * 获取手机尺寸（不包括虚拟按钮部分）
     *
     * @param context
     * @return
     */
    public static DisplayMetrics getMetrics(Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        DisplayMetrics displayMetrics = new DisplayMetrics();
        display.getMetrics(displayMetrics);

        int phoneWidth = displayMetrics.widthPixels;
        int phoneHeight = displayMetrics.heightPixels;
        LogUtil.i("666","phoneWidth:" + phoneWidth + "--phoneHeight:" + phoneHeight);
        return displayMetrics;
    }

    /**
     * 最小的屏幕的宽度
     * @param context
     * @return
     */
    public static int getSmallestScreenWidth(Context context){
        Configuration config = context.getResources().getConfiguration();
        int smallestScreenWidth = config.smallestScreenWidthDp;
        LoggerUtil.e("smallest width : " + smallestScreenWidth);
        return smallestScreenWidth;
    }

    /**
     * 隐藏旋转遮罩
     *
     * @param waitRegView
     */
    public static void hideWaitRegView(final WaitRegView waitRegView) {
        if (waitRegView != null && waitRegView.getVisibility() != View.GONE) {
            waitRegView.post(new Runnable() {
                @Override
                public void run() {
                    waitRegView.setVisibility(GONE);
                }
            });
        }
    }

    /**
     * 隐藏旋转遮罩
     *
     * @param waitRegView
     */
    public static void hideWaitRegViewOnMainThread(final WaitRegView waitRegView) {
        if (waitRegView != null && waitRegView.getVisibility() != View.GONE) {
            waitRegView.setVisibility(GONE);
        }
    }

    /**
     * 显示旋转遮罩
     *
     * @param waitRegView
     */
    public static void showWaitRegView(final WaitRegView waitRegView) {
        if (waitRegView != null) {
            waitRegView.post(new Runnable() {
                @Override
                public void run() {
                    waitRegView.setVisibility(View.VISIBLE);
                }
            });
        }
    }

    /**
     * 显示旋转遮罩
     *
     * @param waitRegView
     */
    public static void showWaitRegViewOnMainThread(final WaitRegView waitRegView) {
        if (waitRegView != null) {
            waitRegView.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 设备改名字
     */
    public static Dialog removeDeviceDialog(Context context, final IOTDevice device) {
        if (device != null) {

            final View view = View.inflate(context, R.layout.dialog_device_rename, null);

            TextView deviceNameTv = (TextView) view.findViewById(R.id.tv_title);
            final EditText nameEt = (EditText) view.findViewById(R.id.et_device_name);
            Button cannelBtn = (Button) view.findViewById(R.id.btn_cancel);
            Button okBtn = (Button) view.findViewById(R.id.btn_ok);
            okBtn.setText(R.string.OK);

            deviceNameTv.setText(R.string.Title_Edit_Device_Name);
            nameEt.setText(device.getName());
            nameEt.setSingleLine();

            final CustomDialog dialog = new CustomDialog(context, R.style.DialogTheme) {
                @Override
                public View getView() {
                    return view;
                }
            };

            okBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    String newName = nameEt.getText().toString();

                    if (!TextUtils.isEmpty(newName)) {
                        device.setName(newName);
                        dialog.dismiss();
                    }
                }
            });

            cannelBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.cancel();
                }
            });

            dialog.show();

            return dialog;
        }
        return null;
    }

    /**
     * 设备改名字
     */
    public static Dialog renameArmZoneDialog(Context context, final IOTSecurityArm device, final int armZoneIndex) {
        if (device != null) {

            final View view = View.inflate(context, R.layout.dialog_device_rename, null);

            TextView deviceNameTv = (TextView) view.findViewById(R.id.tv_title);
            final EditText nameEt = (EditText) view.findViewById(R.id.et_device_name);
            Button cannelBtn = (Button) view.findViewById(R.id.btn_cancel);
            Button okBtn = (Button) view.findViewById(R.id.btn_ok);
            okBtn.setText(R.string.OK);

            deviceNameTv.setText(R.string.Title_Edit_Device_Name);
            nameEt.setText(device.getArmZoneInfoMap().get(armZoneIndex).getName());
            nameEt.setSingleLine();

            final CustomDialog dialog = new CustomDialog(context, R.style.DialogTheme) {
                @Override
                public View getView() {
                    return view;
                }
            };

            okBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    String newName = nameEt.getText().toString();

                    if (!TextUtils.isEmpty(newName)) {
                        //远程保存
                        device.setName(armZoneIndex,newName);
                        //本地保存
                        device.getArmZoneInfoMap().get(armZoneIndex).setName(newName);
                        device.onNameChangeEvent();
                        dialog.dismiss();
                    }

                }
            });

            cannelBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.cancel();
                }
            });

            dialog.show();

            return dialog;
        }
        return null;
    }

    public interface OnEnterInputListener{
        void onEnterInput(String text);
        void onCancel();
    }

    /**
     * 获取用户的输入
     * @param context   上下文
     * @param listener  输入回调
     * @param hintTextId    dialog的提示
     * @param type      dialog的类型, -1为普通dialog, 否则为全局dialog, 即context为application级别的
     */
    public static void getInputDialogDialog(Context context, final OnEnterInputListener listener, int hintTextId, int type) {
            final View view = View.inflate(context, R.layout.dialog_device_rename, null);

            TextView deviceNameTv = (TextView) view.findViewById(R.id.tv_title);
            final EditText nameEt = (EditText) view.findViewById(R.id.et_device_name);
            Button cannelBtn = (Button) view.findViewById(R.id.btn_cancel);
            Button okBtn = (Button) view.findViewById(R.id.btn_ok);
            okBtn.setText(R.string.OK);

            deviceNameTv.setText(hintTextId);
            nameEt.setSingleLine();

            final CustomDialog dialog = new CustomDialog(context, R.style.DialogTheme) {
                @Override
                public View getView() {
                    return view;
                }
            };

            okBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    String newName = nameEt.getText().toString();

                    if (!TextUtils.isEmpty(newName)) {
                        if(listener != null){
                            listener.onEnterInput(newName);
                        }
                        dialog.dismiss();
                    }
                }
            });

            cannelBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            if(type != -1){
                dialog.getWindow().setType(type);
            }
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                listener.onCancel();
            }
        });
            dialog.show();

        }

    public static int getSecurityLineState(IOTSecurityArm securityArm) {

        List<ArmZoneInfoBean> onUseArmZoneInfoList = securityArm.getOnUseArmZoneInfoList();
        int size = onUseArmZoneInfoList.size();

        String warningTemp = App.getContext().getResources().getString(R.string.warning_arm);

        ArrayList<String> nameList = new ArrayList<>();

        for (int i = 0; i < size; i++) {
            ArmZoneInfoBean armZoneInfoBean = onUseArmZoneInfoList.get(i);
            int lineState = armZoneInfoBean.getLineState();
            if (lineState == 1) {
                String name = armZoneInfoBean.getName();
                if (!nameList.contains(name)){
                    nameList.add(name);
                }
            }
        }

        int nameCount = nameList.size();

        if (nameCount > 0){

            String nameFinal = "";

            for (int i = 0; i < nameCount; i++) {
                if (i < nameCount - 1) {
                    nameFinal = nameFinal + nameList.get(i) + ",";
                } else {
                    nameFinal = nameFinal + nameList.get(i);
                }
            }

            final String warning = String.format(warningTemp, nameFinal);

            UIUtils.postTaskSafely(new Runnable() {
                @Override
                public void run() {
                    ToastUtil.showLongToastCenter(warning);
                }
            });
        }

        return nameCount;
    }

}
